/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vistas;

import com.util.Automata;
import com.util.ExampleFileFilter;
import com.util.LeerXml;
import converautomatas.ConverAutomatas;
import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public class CargarXml extends javax.swing.JDialog {

    private Automata a;

    public Automata getA() {
        return a;
    }

    public void setA(Automata a) {
        this.a = a;
    }

    /**
     * Creates new form CargarXml
     */
    public CargarXml(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        a = new Automata();
    }

    public CargarXml(java.awt.Frame parent, boolean modal, Automata automata) {
        super(parent, modal);
        initComponents();
        a = automata;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlArchivo = new javax.swing.JLabel();
        jtfRuta = new javax.swing.JTextField();
        jbExaminar = new javax.swing.JButton();
        jbCargar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jlArchivo.setText("Archivo:");

        jbExaminar.setText("Examinar...");
        jbExaminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExaminarActionPerformed(evt);
            }
        });

        jbCargar.setText("Cargar");
        jbCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jlArchivo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfRuta, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbExaminar)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbCargar)
                .addGap(181, 181, 181))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlArchivo)
                    .addComponent(jtfRuta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbExaminar))
                .addGap(18, 18, 18)
                .addComponent(jbCargar)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbExaminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExaminarActionPerformed
        JFileChooser jFileChooser = new JFileChooser();
        ExampleFileFilter filter = new ExampleFileFilter(new String[]{"XML", "xml"}, "");
        jFileChooser.setFileFilter(filter);
        jFileChooser.setAcceptAllFileFilterUsed(false);
        int opt = jFileChooser.showOpenDialog(this);
        if (opt == JFileChooser.APPROVE_OPTION) {
            jtfRuta.setText(jFileChooser.getSelectedFile().getPath());
        }
    }//GEN-LAST:event_jbExaminarActionPerformed

    private void jbCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCargarActionPerformed
        cargarXml();
    }//GEN-LAST:event_jbCargarActionPerformed

    public Automata cargarXml() {
        a = LeerXml.cargarXml(jtfRuta.getText());
        this.setVisible(false);
        ViewInicio inicio = ConverAutomatas.getInicio();
        inicio.setA(a);
        inicio.getjTNoDeterminista().setModel(new DefaultTableModel());
        inicio.getjTDeterminista().setModel(new DefaultTableModel());
        inicio.getjTNoDeterminista().setVisible(true);
//        jTDeterminista.setVisible(false);
        inicio.cargarAutomata(a, (DefaultTableModel) inicio.getjTNoDeterminista().getModel());
        return a;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CargarXml.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CargarXml.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CargarXml.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CargarXml.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CargarXml dialog = new CargarXml(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbCargar;
    private javax.swing.JButton jbExaminar;
    private javax.swing.JLabel jlArchivo;
    private javax.swing.JTextField jtfRuta;
    // End of variables declaration//GEN-END:variables
}
